package com.wkhyapp.lm.view;

import android.graphics.Color;
import android.os.Build;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.loadmore.LoadMoreView;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.adapter.NoticeAdapter;
import com.wkhyapp.lm.base.SuperActivity;
import com.wkhyapp.lm.contract.NoticePresenter;
import com.wkhyapp.lm.contract.NoticeView;
import com.wkhyapp.lm.http.vo.Notice;
import com.wkhyapp.lm.utils.StatusBarFontUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class NoticeActivity extends SuperActivity<NoticePresenter> implements NoticeView {


    @BindView(R.id.refresh_view)
    SwipeRefreshLayout refresh_view;
    @BindView(R.id.recycler_view)
    RecyclerView recycler_view;
    private NoticeAdapter noticeAdapter;
    private List<Notice> notices;
    private int mCurrentPage = 1;
    @BindView(R.id.back_iv)
    ImageView back_iv;


    @Override
    public void initUI() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            translucentStatusBar();
        }
        StatusBarFontUtil.setLightStatusBarColor(this);


        notices = new ArrayList<>();
        noticeAdapter = new NoticeAdapter(R.layout.item_notice,notices);
        refresh_view.setColorSchemeColors(Color.parseColor("#FFEC48"),Color.parseColor("#FFEC48"));
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recycler_view.setLayoutManager(manager);
        recycler_view.setAdapter(noticeAdapter);
        refresh_view.setRefreshing(false);
        mPresenter.getNotic();
    }

    @Override
    protected void initListener() {
        noticeAdapter.setLoadMoreView(new LoadMoreView() {
            @Override
            public int getLayoutId() {
                return R.layout.view_index_load_more;
            }

            @Override
            protected int getLoadingViewId() {
                return com.chad.library.R.id.load_more_loading_view;
            }
            @Override
            protected int getLoadFailViewId() {
                return com.chad.library.R.id.load_more_load_fail_view;
            }
            @Override
            protected int getLoadEndViewId() {
                return com.chad.library.R.id.load_more_load_end_view;
            }
        });

        noticeAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mCurrentPage = mCurrentPage+1;
                mPresenter.getNoticMore(mCurrentPage);

            }
        });
        refresh_view.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mCurrentPage = 1;
                mPresenter.getNotic();
            }
        });


        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void setNotice(List<Notice> list) {
        notices.clear();
        if(list!=null&&list.size()>0){
            notices.addAll(list);
        }
        noticeAdapter.notifyDataSetChanged();
        refresh_view.setRefreshing(false);
        noticeAdapter.loadMoreComplete();

    }

    @Override
    public void setNoticeMore(List<Notice> list) {
        if(list.size()==0){
            noticeAdapter.loadMoreComplete();
            noticeAdapter.loadMoreEnd();
        }else{
            notices.addAll(list);
            noticeAdapter.notifyDataSetChanged();
            noticeAdapter.loadMoreComplete();
        }
    }

    @Override
    protected NoticePresenter createPresenter() {
        return mPresenter = new NoticePresenter(this);
    }

    @Override
    protected void afterCreate() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_notice;
    }


}
